/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.transport.mvp;

import org.kad.gwt.web.client.kademelia.model.rpc.KadRpcRequest;
import org.kad.gwt.web.client.kademelia.model.rpc.KadRpcResponse;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

/**
 * Fired when the event source receive a response from the kad network.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class KadResponseEvent extends GwtEvent<KadResponseEvent.Handler> {
    public interface Handler extends EventHandler {
        void onKadResponse(KadResponseEvent event);
    }

    /**
     * The event type.
     */
    static Type<KadResponseEvent.Handler> TYPE;

    /**
     * Fires an {@link KadResponseEvent} on all registered handlers in the handler source.
     * 
     * @param <S>
     *            The handler source type
     * @param source
     *            the source of the handlers
     * @param request
     *            {@link #request}
     * @param response
     *            {@link #response}
     * @return The event
     */
    public static <S extends HasKadRequestHandlers> KadResponseEvent fire(S source, KadRpcRequest request, KadRpcResponse response) {
        KadResponseEvent event = null;
        if (TYPE != null) {
            event = new KadResponseEvent(request, response);
            source.fireEvent(event);
        }
        return event;
    }

    /**
     * Ensures the existence of the handler hook and then returns it.
     * 
     * @return returns a handler hook
     */
    public static Type<KadResponseEvent.Handler> getType() {
        if (TYPE == null) {
            TYPE = new Type<KadResponseEvent.Handler>();
        }
        return TYPE;
    }

    /**
     * The associated request message.
     */
    private final KadRpcRequest request;
    /**
     * The associated request response.
     */
    private KadRpcResponse      response;

    /**
     * Construct a new {@link KadResponseEvent}.
     */
    protected KadResponseEvent(KadRpcRequest requestIn, KadRpcResponse responseIn) {
        this.request = requestIn;
        this.response = responseIn;
    }

    /**
     * @return {@link #request}
     */
    public KadRpcRequest getRequest() {
        return request;
    }

    /**
     * @return {@link #response}
     */
    public KadRpcResponse getResponse() {
        return response;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Type<KadResponseEvent.Handler> getAssociatedType() {
        return TYPE;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dispatch(KadResponseEvent.Handler handler) {
        handler.onKadResponse(this);
    }
}
